﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.Announcement.SearchAnnounce
{
    public partial class SearchAnnounceUserControl : UserControl
    {
        public int _userId
        {
            set { ViewState["_userId"] = value; }
            get { return (int)ViewState["_userId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ApplicationContext.Current.GetKeyValue("userId") != null)
            {
                _userId = Int32.Parse(ApplicationContext.Current.GetKeyValue("userId").ToString());
            }

            if (!IsPostBack)
            {
                
            }
        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            string subject = this.txtSubject.Text;

            DateTime? start = null;
            DateTime? end = null;

            if (!dtEffDateFrom.IsDateEmpty)
                start = this.dtEffDateFrom.SelectedDate;

            if (!dtEffDateTo.IsDateEmpty)
                end = this.dtEffDateTo.SelectedDate;

            using (AnnounceProxy proxy = new AnnounceProxy())
            {
                AnnounceDataSet.T_Announce_Search_By_UserDataTable searchDT = proxy.SearchAnnounceByUser(subject, start, end, _userId);
                this.gvBC.DataSource = searchDT;
                this.gvBC.DataBind();
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(SPContext.Current.Web.Url);
        }

        protected void gvBC_DataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // load date format
                Label lblEffDate = (Label)e.Row.Cells[0].FindControl("lblEffDate");

                string dateString = lblEffDate.Text;
                DateTime dateTime = DateTime.Parse(dateString);
                lblEffDate.Text = dateTime.ToString(JCYConstant.Date_Format_CN);
            }

        }
    }
}
